<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-user-add">
			<input name="yhqType"  type="hidden" id="treeId"/>

			<div class="form-group">
				<label class="col-sm-3 control-label">优惠类型：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="yhqTypeName" onclick="selectTypeTree()" readonly="true" id="treeName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">数量：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="yhqAmount" id="yhqAmount">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">有效期：</label>
				<div class="col-sm-8">
					<li class="select-time">
						<input type="text" class="time-input" id="startTime" placeholder="开始时间" name="startTime"/>
						<span>-</span>
						<input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
					</li>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">折扣额：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="yhqDiscount" id="yhqDiscount">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">时长卡时长：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="yhqHours" id="yhqHours">
				</div>
			</div>


			<div class="form-group">
				<label class="col-sm-3 control-label">状态5：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
                         <input type="checkbox" th:checked="false" class="onoffswitch-checkbox" id="yhqState" name="yhqState">
                         <label class="onoffswitch-label" for="yhqState">
                             <span class="onoffswitch-inner"></span>
                             <span class="onoffswitch-switch"></span>
                         </label>
                     </div>
				</div>
			</div>

		</form>
	</div>
	<div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/select/select2.js}"></script>
	<script>
        $("#form-user-add").validate({
        	rules:{

				yhqTypeName:{
        			required:true,
        		},
        		yhqAmount:{
        			required:true,
        		},
				startTime:{
					required:true,
				},
				endTime:{
					required:true,
				},

        	},

        });
        
        function submitHandler() {
	        if ($.validate.form()) {
	        	add();
	        }
	    }

        function add() {
        	var yhqType = $("input[name='yhqType']").val();
        	var yhqAmount = $("input[name='yhqAmount']").val();
        	var startTime = $("input[name='startTime']").val();
        	var endTime = $("input[name='endTime']").val();
			var yhqDiscount = $("input[name='yhqDiscount']").val();
        	var yhqHours = $("input[name='yhqHours']").val();
        	var yhqState = $("input[name='yhqState']").is(':checked') == true ? 1 : 0;
        	// var sex = $("#sex option:selected").val();
        	$.ajax({
        		cache : true,
        		type : "POST",
        		url : ctx + "gxgw_modules/yhq/add",
        		data : {
        			"yhqType": yhqType,
        			"yhqAmount": yhqAmount,
        			"startTime": startTime,
        			"endTime": endTime,
					"yhqDiscount": yhqDiscount,
        			"yhqHours": yhqHours,
        			"yhqState": yhqState,
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.successCallback(data);
        		}
        	});
        }

        /*优惠类型-新增-选择类型树*/
        function selectTypeTree() {
        	var treeId = $("#treeId").val();
        	var yhqType = $.common.isEmpty(treeId) ? "1" : $("#treeId").val();
        	var url = ctx + "gxgw_modules/yhqType/selectTypeTree/" + yhqType;
			var options = {
				title: '选择部门',
				width: "380",
				url: ctx + "gxgw_modules/yhqType/selectTypeTree/" + yhqType,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowParents(tree)) {
				var body = layer.getChildFrame('body', index);
    			$("#treeId").val(body.find('#treeId').val());
    			$("#treeName").val(body.find('#treeName').val());
    			layer.close(index);
			}
		}
    </script>
</body>
</html>
